package android.fwirk;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class CameraActivity extends Activity implements SurfaceHolder.Callback, OnClickListener {
	SurfaceView sv;
	SurfaceHolder sh;
	Camera camera;
	Button capture;
	Button back;
	boolean mPreviewRunning=true;
	PictureCallback pictureCallback;
	AlertDialog confirmDialog;
	int DIALOG_CONFIRM=0;
	@Override
	public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.camera);
    	 getWindow().setFormat(PixelFormat.TRANSLUCENT);
    	 setContentView(R.layout.camera);
    	 sv = (SurfaceView) findViewById(R.id.cameraview);
    	 capture = (Button) findViewById(R.id.captureb);
    	 back = (Button) findViewById(R.id.backb);
    	 capture.setOnClickListener(this);
    	 back.setOnClickListener(this);
    	 sh = sv.getHolder();
    	 sh.addCallback(this);
    	 sh.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    	 Builder bd=new Builder(this);
    	 confirmDialog = bd.setMessage("Upload this picture?").setPositiveButton("Yes",
 				new DialogInterface.OnClickListener() {
 					public void onClick(DialogInterface dialog, int which) {
 						if (which == DialogInterface.BUTTON_POSITIVE) {
 							if(camera!=null) {
 								camera.release();
 							}
 							setResult(Activity.RESULT_OK);
 			    			finish();
 						}

 					}
 				}).setNegativeButton("No",
 				new DialogInterface.OnClickListener() {
 					public void onClick(DialogInterface dialog, int which) {
 						if (which == DialogInterface.BUTTON_NEGATIVE) {
 							Const.curImgData=null;
 							camera.startPreview();
 							dialog.dismiss();
 						}

 					}
 				}).create();

    	 pictureCallback = new Camera.PictureCallback() {
    		 public void onPictureTaken(byte[] imageData, Camera c) {
    			 Log.d("Test picture taken","true");
    			 Const.curImgData=imageData;
    			 showDialog(DIALOG_CONFIRM);
    		 }
    		 };

		Const.curImgData=null;
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		if(id==DIALOG_CONFIRM) {
			return confirmDialog;
		}
		return null;
	}
	
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
			if (mPreviewRunning) {
				camera.stopPreview();
			}
			Camera.Parameters p = camera.getParameters();
			p.setPreviewSize(width, height);
			camera.setParameters(p);
			try {
			camera.setPreviewDisplay(holder);
			} catch (IOException e) {
				e.printStackTrace();
			}
			camera.startPreview();
			mPreviewRunning = true;

	}

	public void surfaceCreated(SurfaceHolder holder) {
		try {
			camera=Camera.open();
		}
		catch (Exception e) {
			Const.curImgData=null;
			setResult(Activity.RESULT_CANCELED);
			finish();
		}
		
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		camera.stopPreview();
		mPreviewRunning = false;
		if(camera!=null) {
			camera.release();
		}
	}

	public void onClick(View arg0) {
		if(arg0==capture) {
			camera.takePicture(null, null, pictureCallback);
		}
		else if(arg0==back) {
			Const.curImgData=null;
			setResult(Activity.RESULT_CANCELED);
			finish();
		}

	}	
}